// vp_cai/pages/index/areas.js
var app = getApp();
var vp = require('../../resource/js/vp.js');
// pages/medical-records/index.js
Page({
  data: {
    tabs: [
      { type: 'outpatient', name: '门诊病历' },
      { type: 'prescription', name: '门诊处方' },
      { type: 'discharge', name: '出院记录' },
      { type: 'physical', name: '体检报告' },
      { type: 'report', name: '报告单' }
    ],
    activeTab: 'outpatient',
    scrollLeft: 0,
    records: [],
    groupedRecords: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    loading: false,
	activeUser:{},
  },

  onLoad(options) {
	  this.setData({
		  activeUser:wx.getStorageSync('activeUser')
	  })
    // 从上一个页面接收参数，展示指定tab
    if (options.tab) {
      const tabIndex = this.data.tabs.findIndex(item => item.type === options.tab);
      if (tabIndex !== -1) {
        this.setData({
          activeTab: options.tab,
          scrollLeft: tabIndex * 100 // 滚动到指定tab位置
        });
      }
    }
    this.loadData();
  },

  // 加载数据
  loadData() {
    if (this.data.loading || !this.data.hasMore) return;
    
    this.setData({ loading: true });
    var that = this
      app.util.request({
    	  url: 'Medicalmanage/getMedicalNote',
    	  data: {
    		  'family_id':this.data.activeUser.id,
    		  'page':that.data.page,
    		  'size':this.data.pageSize,
    		  'type':this.data.activeTab
    	  },
    	  success(res) {
    		  var data = res.data.data;
    		  console.log(data)
    		  that.setData({
    			  'groupedRecords':data,
				  'page': this.data.page + 1,
				   'hasMore': data.total > this.data.pageSize * this.data.page,
				  'loading': false
    		  })
    	  }
      })
  },


  // 切换tab
  switchTab(e) {
    const type = e.currentTarget.dataset.type;
    if (this.data.activeTab === type) return;
    this.setData({
      activeTab: type,
      groupedRecords: [],
      page: 1,
      hasMore: true,
      loading: false
    });
    this.loadData();
  },

  // 加载更多
  loadMore() {
    if (!this.data.loading && this.data.hasMore) {
      this.loadData();
    }
  },
 gotoUpload(){
	 wx.navigateTo({
	     url: `/vp_pz/pages/familydoctor/uploadmedical?type=${this.data.activeTab}`
	   });
 },
  // 显示删除确认对话框
   showDeleteConfirm: function(e) {
     const id = e.currentTarget.dataset.id;
     const date = e.currentTarget.dataset.date;
     
     wx.showModal({
       title: '删除确认',
       content: '确定要删除这条记录吗？',
       confirmColor: '#ff4d4f',
       success: (res) => {
         if (res.confirm) {
           this.deleteRecord(id, date);
         }
       }
     });
   },
   
   // 执行删除操作
   deleteRecord: function(id, date) {
     wx.showLoading({ title: '删除中...', mask: true });
     var that = this
       app.util.request({
     	  url: 'Medicalmanage/delMedicalNote',
     	  data: {
     		  'id':id
     	  },
     	  success(res) {
			  that.setData({
			    page: that.data.page,
			    hasMore: true,
			    loading: false
			  });
			  that.loadData()
     	  }
       })
     
   },
  // 预览图片
  previewImage(e) {
    const urls = e.currentTarget.dataset.urls;
    const index = e.currentTarget.dataset.index;
    
    wx.previewImage({
      current: urls[index],
      urls: urls
    });
  },
});